let activeEffect = null;
export const effect = (fn) => {
  const _effect = () => {
    activeEffect = _effect;
    fn();
  };
  _effect();
  return _effect;
};

const targetMap = new WeakMap();
export const track = (target, key) => {
  let depsMap = targetMap.get(target);
  if (!depsMap) {
    depsMap = new Map();
    targetMap.set(target, depsMap);
  }
  let deps = depsMap.get(key);
  if (!deps) {
    deps = new Set();
    depsMap.set(key, deps);
  }
  deps.add(activeEffect);
};
export const trigger = (target, key) => {
  let depsMap = targetMap.get(target);
  if (!depsMap) {
    return;
  }
  let deps = depsMap.get(key);
  deps.forEach((_fn) => _fn());
};
